//
//  LoginViewController.m
//  Bmob
//
//  Created by xalo on 16/7/13.
//  Copyright © 2016年 Hero. All rights reserved.
//

#import "LoginViewController.h"

@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *passWord;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithRed:169/255.0 green:199/255.0 blue:137/255.0 alpha:1];
   
    
}

- (IBAction)cancel:(UIButton *)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)loginAction:(id)sender {
    
    if (self.userName.text.length == 0) {
        [self showAlertViewWithMessage:@"请输入用户名"];
        return;
    }
    if (self.passWord.text.length == 0 ) {
        [self showAlertViewWithMessage:@"请输入七位以上的密码"];
        return;
    }
    
    [BmobUser loginWithUsernameInBackground:self.userName.text password:self.passWord.text block:^(BmobUser *user, NSError *error) {
        if (user) {
            NSLog(@"登陆成功");
//            [self dismissViewControllerAnimated:YES completion:NULL];
            [self.navigationController popViewControllerAnimated:YES];
            //键盘回收
            
            [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
        }else{
            NSLog(@"登陆失败");
            [self showAlertViewWithMessage:@"登陆失败"];
        }
    }];
}

//创建弹窗
- (void)showAlertViewWithMessage:(NSString *)message{
    
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil];
    
    [alertView show];
}

@end
